<?php
/**
 * Pages_Model_Instructor
 *
 */
class Zend_Instructor extends Zend_Db_Table_Abstract 
{
    protected $_name = 'T_STUDENTS_GROUPS';
    protected $_name_tasks = 'T_PG_TASKS_GROUPS';
    protected $_name_teach = 'T_TEACHERS';
    protected $_name_pass = 'T_PASS';


    /**
     * Get groups of current teacher
     * 
     * @param int $instId
     * @param int $instYear
     */
    public function getGroups($instId, $instYear = NULL) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name)
            ->order('NAME');
        $select->where('T_STUDENTS_GROUPS.TEACHER_ID = ?', $instId); 
        if ($instYear != NULL) 
        {
            $select->where('T_STUDENTS_GROUPS.START_YEAR = ?', $instYear);
        }
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetchAll();
        return $result;
    }

    /**
     * Get group data
     * 
     * @param int $groupId
     */
    public function getGroup($groupId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name)
            ->order('NAME');
        $select->where('T_STUDENTS_GROUPS.ID = ?', $groupId); 
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetchAll();
        return $result;
    }

    /**
     * Check if login and pass is true
     * 
     * @param string $login
     * @param string $pass
     */
    public function checkPass($login, $pass) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name_pass);
        $select->where('T_PASS.LOGIN = ?', $login);
        $select->where('T_PASS.PASS = ?', $pass);
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetch();
        return $result;
    }

    /**
     * Get students of current group
     * 
     * @param int $groupId
     */
    public function getStudents($groupId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name, array('NAME', 'START_YEAR'))
            ->join('T_STUDENTS', 'T_STUDENTS.GROUP_ID = T_STUDENTS_GROUPS.ID', array('ID', 'NAME_FIRST', 'NAME_MIDDLE', 'NAME_LAST', 'BOOK_NUMBER'))
            ->order('NAME_LAST');
        $select->where('T_STUDENTS_GROUPS.ID = ?', $groupId); 
        $stmt = $this->getAdapter()->query($select);
        $result = $stmt->fetchAll();
        return $result;
    }


    /**
     * Check if student have a selected task and return date of assignment if it's true
     * 
     * @param int $studentId
     */
    public function checkStudent($studentId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name_tasks, array('ASSIGMENT_DATE'));
            $select->where('T_PG_TASKS_GROUPS.RESERVED_FOR = ?', $studentId); 
            $stmt = $this->getAdapter()->query($select);
            $result = $stmt->fetch();
        if ($result == NULL)
        return 'No';
        else return $result ['ASSIGMENT_DATE'];
    }

    /**
     * Get teacher data
     * 
     * @param int $instId
     */
    public function getInst($instId) 
    {
        $select = $this->getAdapter()->select()
            ->from($this->_name_teach)
            ->limit(1);
            $select->where('T_TEACHERS.ID = ?', $instId); 
            $stmt = $this->getAdapter()->query($select);
            $result = $stmt->fetchAll();
        return $result;
    }
}